#!/bin/bash

SHELL_DIR="$(cd $(dirname $0); pwd)"
PROJECT_DIR=$1
SYSTEM_DIR=$2
system_folder_name=`ls *system* -d`
if [[ ! -d ${SYSTEM_DIR} ]]; then
	echo "无SYSTEM目录，爪巴！"
	exit 1
fi
#echo "当前项目目工作录 :$PROJECT_DIR" 
#echo "插件目录 : $SHELL_DIR"
#echo "当前项目刷机包的system目录 :$SYSTEM_DIR"
#echo -e "\e[36m      >>>>>>>>>>     .     >>>>>>>>>> \e[0m"
echo ""
echo ""

lin_dir=${PROJECT_DIR##*/}
if [ ! -d $SHELL_DIR/$lin_dir  ];then
  mkdir $SHELL_DIR/$lin_dir
fi

if [[ -f $SHELL_DIR/Insides/apktool/apktool.jar ]]; then
	ApkTool="java -jar $SHELL_DIR/Insides/apktool/apktool.jar" 
	echo "ApkTool version:" && $ApkTool -version
	
else
	echo -e "\e[36m     >>>>>>>>>   没有找到apktool   >>>>>>>> \e[0m"
	echo "   ----->请在$SHELL_DIR/Insides/apktool目录下查看是否有apktool "
	sleep 10
	exit 1
fi

read -p "请问当前刷机包是否是MIUI13 【1】是，【2】不是: " is_miui13
if [ $is_miui13 -eq 1 ];then
	echo -e "\e[36m      >>>>>>>>>>>   MIUI13无需破解卡米     >>>>>>>>>>> \e[0m"
	echo -e "\e[36m      >>>>>>>>>>>   MIUI13无需破解卡米     >>>>>>>>>>> \e[0m"
	echo -e "\e[36m      >>>>>>>>>>>   MIUI13无需破解卡米     >>>>>>>>>>> \e[0m"
	echo -e "\e[36m      >>>>>>>>>>>   MIUI13无需破解卡米     >>>>>>>>>>> \e[0m"
	echo -e "\e[36m      >>>>>>>>>>>   MIUI13无需破解卡米     >>>>>>>>>>> \e[0m"
	sleep 5
	exit 0
elif [ $is_miui13 -eq 2 ];then
	if [[ -f $SYSTEM_DIR/framework/services.jar ]]; then
	
		if [[ $(grep "classes.dex" $SYSTEM_DIR/framework/services.jar) == "" ]]; then	
			cp $SYSTEM_DIR/framework/services.jar $SHELL_DIR/$lin_dir/services.jar.bak
			$ApkTool d -q -r -f -o $SHELL_DIR/services $SYSTEM_DIR/framework/services.jar

			if [ -d $SHELL_DIR/services ]; then
				echo -e "\e[36m    ------------->正在去除卡米    \e[0m"
				echo ""
				echo ""
				Crack_File=$(find $SHELL_DIR/services/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl '.method private checkSystemSelfProtection(Z)V' | sed 's/^\.\///' | sort)
				sed -i '/^.method private checkSystemSelfProtection(Z)V/,/^.end method/{//!d}' $Crack_File
				sed -i -e '/^.method private checkSystemSelfProtection(Z)V/a\    .locals 1\n\n    return-void' $Crack_File

				
				$ApkTool b -q -f -o $SYSTEM_DIR/framework/services.jar $SHELL_DIR/services
				${su}rm -rf $SHELL_DIR/services 2>/dev/null
				echo -e "\e[36m        去除卡米成功<-----------------    \e[0m"	
				sleep 3			
			fi

		
		else
		echo -e "\e[36m    >>>>>>>>>   没有找到相关卡米文件  >>>>>>>> \e[0m"
		sleep 5
		exit 0
		fi
	fi			
else
	echo -e "\e[36m    >>>>>>>>>    看不懂提示？非得找骂？   >>>>>>>> \e[0m"
	echo -e "\e[36m    >>>>>>>>>    看不懂提示？非得找骂？   >>>>>>>> \e[0m"
	echo -e "\e[36m    >>>>>>>>>    看不懂提示？非得找骂？   >>>>>>>> \e[0m"
	echo -e "\e[36m    >>>>>>>>>    看不懂提示？非得找骂？   >>>>>>>> \e[0m"
	echo -e "\e[36m    >>>>>>>>>    看不懂提示？非得找骂？   >>>>>>>> \e[0m"
	sleep 5
	exit 1
fi


#chmod -R 0777 $PROJECT_DIR
echo -e "\e[32mDone\e[0m"


